﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Configurations;
using System.Data;
using System.Xml.Serialization;

namespace Pixysoft.Framework.Noebe.MemoryCaches.Core
{
    class CacheDataRowCell : ICacheDataRowCell
    {
        string columnName;
        string columnValue;
        string dataType;

        public CacheDataRowCell()
        {
        }

        public CacheDataRowCell(string columnName, string columnType)
        {
            this.columnName = columnName;
            this.dataType = columnType;
        }

        public CacheDataRowCell(string columnName, object columnValue)
        {
            this.columnName = columnName;

            this.DataType = columnValue.GetType().FullName;

            switch (Pixysoft.Tools.ParserHelper.GetDataType(columnValue))
            {
                case DotNetDataType.Byte:
                    {
                        this.columnValue =
                            Pixysoft.Security.Base64.XEncript(
                            columnValue as byte[]);

                        break;
                    }
                case DotNetDataType.Char:
                    {
                        this.ColumnValue = ((int)((char)columnValue)).ToString();

                        break;
                    }
                default:
                    {
                        this.columnValue = columnValue.ToString();

                        break;
                    }

            }
        }

        public object GetColumnValue()
        {
            switch (Pixysoft.Tools.ParserHelper.GetDataTypeByTypeName(dataType))
            {
                case DotNetDataType.Byte:
                    {
                        try
                        {
                            return Pixysoft.Security.Base64.XDecript(
                                columnValue);
                        }
                        catch (Exception ex)
                        {
                            LoggerHelper.Fatal("error when parse byte[] in get column value");

                            LoggerHelper.Fatal(ex);

                            return DBNull.Value;
                        }
                    }
                case DotNetDataType.Char:
                    {
                        try
                        {
                            return (char)int.Parse(ColumnValue);
                        }
                        catch (Exception ex)
                        {
                            LoggerHelper.Fatal("error when parse char in get column value");

                            LoggerHelper.Fatal(ex);

                            return DBNull.Value;
                        }
                    }
                default:
                    {
                        //由于double无法parse double.Maxvalue，所以会有个错误在这里

                        object rvalue = null;

                        if (!Pixysoft.Tools.ParserHelper.TryParse(dataType, columnValue, out rvalue, null))
                            return DBNull.Value;

                        return rvalue;
                    }
            }
        }

        [XmlAttribute("columnname")]
        public string ColumnName
        {
            get { return columnName; }
            set { columnName = value; }
        }


        public string ColumnValue
        {
            get { return columnValue; }
            set { columnValue = value; }
        }

        [XmlAttribute("datatype")]
        public string DataType
        {
            get { return dataType; }
            set { dataType = value; }
        }
    }
}
